package helpers;

public class FinCalc {

	public static long calculateAnuity (long desiredFutureValue, double interestRate, int numPeriods) {
		long anuity = 0; 
		
		double interestFactor = (Math.pow(1+interestRate,numPeriods)-1)/interestRate;
		anuity = (long) (Math.ceil(desiredFutureValue/interestFactor)+0.1); //Add the 0.1 to ensure that 1.99999986 becomes 2
	
		return anuity;
	}
	
	public static long calculateFutureValueOfAnuity (long anuity, double interestRate, int numPeriods) {
		long futureValue = 0; //The anuity
		
		double interestFactor = (Math.pow(1+interestRate,numPeriods)-1)/interestRate;
		futureValue = (long) (Math.ceil(anuity*interestFactor)+0.1); //Add the 0.1 to ensure that 1.99999986 becomes 2
	
		return futureValue;
	}
	
	public static double simpleToCompoundingInterestRate (double interestRate, double compoundingPeriod, double simpleInterestPeriod) {
	
		return Math.pow(1+(compoundingPeriod/simpleInterestPeriod),simpleInterestPeriod)-1.0;
		
	}
	
	//http://en.wikipedia.org/wiki/Future_value
	/*
	public static double convertSimpleInterestRatePeriod (double interestRate, double compoundingPeriod, double simpleInterestPeriod) {
			// TODO finish if needed
	}*/
	
	public static long calculateFutureValueOfPresentValue (long presentvalue, double interestRate, int numPeriods) {
		return (long) (presentvalue * Math.pow((1.0 + interestRate), numPeriods));
	}
	
	// TODO exception throwing
	public static long calculateAnuity (long presentvalue, long desiredFutureValue, double interestRate, int numPeriods) {
		long moneyNeeded = desiredFutureValue - calculateFutureValueOfPresentValue(presentvalue, interestRate, numPeriods);
		return calculateAnuity (moneyNeeded, interestRate, numPeriods);	
	}
}